package com.jwdf.uda.ui.fragment.user;

import android.content.Context;
import android.os.Bundle;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.MessageNoticeEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserMessageDetailAction;
import com.jwdf.uda.http.action.UserMessageListAction;
import com.jwdf.uda.http.action.UserReadMessageAction;
import com.jwdf.uda.ui.adapter.MessageAdapter;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.dialog.SysMsgAlertDialog;

import retrofit2.Response;

/**
 * 类说明：
 * 作者：huangqiuxin on 2016/5/17 15:43
 * 邮箱：648859026@qq.com
 */
public class MessageFragment extends SimpleListFragment {
    private MessageAdapter mAdapter;

    @Override
    protected void initViews() {
        mAdapter = new MessageAdapter(getActivity());
        mAdapter.setUserMessageClickListener(new MessageAdapter.UserMessageClickListener() {
            @Override
            public void onClick(final Context context, int msgId) {
                if (msgId != 0) {
                    onNewRequestCall(UserMessageDetailAction.newInstance(context, msgId).request(new AHttpService.IResCallback<PbBean.PbResUserGetMessageDetail>() {
                        @Override
                        public void onCallback(int resultCode, Response<PbBean.PbResUserGetMessageDetail> response, PbBean.PbBaseServerInfo baseServerInfo) {
                            if (HttpHelper.showError(context, resultCode, baseServerInfo, R.string.error_net)) {
                                return;
                            }
                            PbBean.PbResUserGetMessageDetail result = response.body();
                            if (result != null) {
                                SysMsgAlertDialog dlg=new SysMsgAlertDialog(context,result.getMessageDetail().getContent());
                                dlg.show();
                            }
                        }
                    }));
                }
            }
        });
        mDataBinding.commonFragmentList.setAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(int position) {

    }

    @Override
    protected void requestData(final boolean refresh) {
        if (refresh && mAdapter.isEmpty()) {
            showProgressDialog();
        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(UserMessageListAction.newInstance(getContext(), offset).request(new AHttpService.IResCallback<PbBean.PbResUserGetMessageList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserGetMessageList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.commonFragmentList.stopRefresh();
                        mDataBinding.commonFragmentList.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }

    @Override
    public void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(MessageNoticeEvent event) {
        if (MessageNoticeEvent.HAS_READ_MESSAGE == event.getEvent()) {
            refreshUserInfo();
        } else if (MessageNoticeEvent.READ_ALL_MESSAGE == event.getEvent()) {
            if (mAdapter.getCount() > 0) {
                readAllMessage();
            }
        }
    }

    private void refreshUserInfo() {
        requestData(true);
        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.REFRESH_USER_INFO));
    }

    private void readAllMessage() {
        showProgressDialog();
        onNewRequestCall(UserReadMessageAction.newInstance(getContext()).request(new AHttpService.IResCallback<PbBean.PbResUserReadMessage>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserReadMessage> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    refreshUserInfo();
                }
            }
        }));
    }

    private void doSuccessResponse(boolean refresh, PbBean.PbResUserGetMessageList body) {
        if (body != null) {
            if (refresh) {
                mAdapter.setData(body.getMessageListList());
            } else {
                mAdapter.append(body.getMessageListList());
            }
            if (body.getMessageListList().size() == 0) {
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
            }
            mDataBinding.commonFragmentList.setPullLoadEnable(body.getMessageListList().size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
}
